package 剑指offer;

/**
 * @description:
 * @author: ywk
 * @date: 2021-04-11
 */
public class 子数组最大乘积 {
    /**
     *  通过两个数组分别存取，最大和最小，因为如果值为负数*max得到是最小值
     * @param arr
     * @return
     */
    public static double maxProduct(double[] arr) {
        if (arr == null || arr.length == 0) {
            return 0;
        }
        double[] maxValue = new double[arr.length];
        double[] minValue = new double[arr.length];
        maxValue[0] = minValue[0] = 1;
        double maxSum = arr[0];
        for (int i = 1; i < arr.length; i++) {
            double val = arr[i];
            if (val > 0) {
                maxValue[i] = Math.max(val, maxValue[i - 1] * val);
                minValue[i] = Math.max(val, minValue[i - 1] * val);
            }else{
                maxValue[i] = Math.max(val, minValue[i - 1] * val);
                minValue[i] = Math.max(val, maxValue[i - 1] * val);
            }
            maxSum = Math.max(maxSum, maxValue[i]);
        }
        return maxSum;
    }
    public static void main(String[] args) {
        System.out.println(maxProduct(new double[]{-2.5,4,0,3,0.5,8,-1}));
    }
}
